package sdk.webview.fmc.com.fmcsdk.util;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class Language {

	private static final Map<Integer, Locale> localeMap = new HashMap<Integer, Locale>();

	static{
		localeMap.put(0, Locale.SIMPLIFIED_CHINESE);
		localeMap.put(1, Locale.ENGLISH);
		localeMap.put(2, Locale.TRADITIONAL_CHINESE);
	}


	/**
	 * 获取对应的Local  2:繁体 0：简体中文   1：English
	 * @param index
	 * @return
	 */
	public static Locale getLocal(int index){
		Locale locale = localeMap.get(index);
		if(locale == null){
			locale = Locale.getDefault();
		}
		return locale;
	}


	/**
	 * 语言切换
	 * @param context
	 * @param locale
	 */
	public static void switchLanguage(Context context, Locale locale){
		Resources res = context.getResources();
		Configuration config = res.getConfiguration();
		DisplayMetrics dm = res.getDisplayMetrics();
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			config.setLocale(locale);
		} else {
			config.locale = locale;
		}

		res.updateConfiguration(config, dm);
	}

}
